package com.geoboard.fragments;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.geoboard.ActivityInterface;
import com.geoboard.FragContainerInterface;
import com.geoboard.R;
import com.geoboard.entity.Board;
import com.geoboard.entity.Post;

public class PostsContainer extends Fragment
							implements FragContainerInterface{
	
	/**** Members **************************************************/
	//private PostsTextFrag textFrag = null;
	private PostsMapFrag mapOverFrag = null;
	
	private Fragment curFragment = null;
	
	private boolean isTextView = false;
	
	private String TAG = this.getClass().getSimpleName();
	private MapFrag mMap;
	private Board mBoard;
	private EditText mEditPostText;
	private Button mPostButton;
	
	ArrayList<Post> posts = null;
	
	
	/**** Methods **************************************************/
	
	private void changeFragment(boolean isTextView){
		FragmentActivity act = getActivity();
		
		if(act != null){
		
			FragmentTransaction ft = act.getSupportFragmentManager().beginTransaction();
			ft.remove(curFragment);
			ft.commit();
			
			if(isTextView){				
				curFragment = new PostsTextFrag();
			} else {							
				PostsMapFrag p = new PostsMapFrag();
				p.setBoard(mBoard);
				curFragment = p;
				
			}
			
			ft = getActivity().getSupportFragmentManager().beginTransaction();
			ft.replace(R.id.posts_content_container,curFragment);
			ft.commit();
			
			
		}
	}
	
	public void setPosts(ArrayList<Post> posts){
		this.posts = posts;
		if(curFragment instanceof PostsTextFrag){
			((PostsTextFrag)curFragment).setPosts(posts);
		}
	}
	
	

	/**** Android Callback Methods *********************************/
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		if(mapOverFrag == null){
			//textFrag = new PostsTextFrag();
			mapOverFrag = new PostsMapFrag();
			
			curFragment = mapOverFrag;
		}
		mMap = ((ActivityInterface)getActivity()).getMapFrag();
		mBoard = (Board) getArguments().getParcelable("com.GeoBoard.entity.board");
		mapOverFrag.setBoard(mBoard);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){		
		
	    // Inflate the layout for this fragment
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.frag_posts_container, container, false);
		mEditPostText = (EditText) root.findViewById(R.id.edtxtMessage);
		mPostButton = (Button) root.findViewById(R.id.btnPost);
		mPostButton.setOnClickListener(createOnClickListener());
		
		
		
		return root;
	}
	
	@Override
	public void onAttach(Activity activity){
		super.onAttach(activity);
	}
	
	@Override
	public void onActivityCreated (Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
		
		
	}
	
	@Override
	public void onViewStateRestored (Bundle savedInstanceState){
		super.onViewStateRestored(savedInstanceState);
	}
	
	@Override
	public void onStart(){
		super.onStart();	
		
	}
	
	@Override
	public void onResume(){
		Log.d(TAG, "onResume");
		super.onResume();		
		changeFragment(isTextView);
		ActivityInterface ai = (ActivityInterface)getActivity();
		ai.setTabState(FragType.POST, isTextView);
		ai.setCurFrag(true);
		if (mBoard != null){
			mMap.getPosts(mBoard);
			TextView tv = (TextView)getActivity().findViewById(R.id.board_name);
			
			if(tv != null){
				tv.setText(mBoard.getName());
			}
			
		}
	}
		
	//why do we do this onPause?
	@Override
	public void onPause(){
		super.onPause();	
		
		getActivity().getSupportFragmentManager().beginTransaction().
		remove(curFragment).commit();
	}
	
	@Override
	public void onStop(){
		super.onStop();
	}
	
	@Override
	public void onDestroyView(){
		super.onDestroyView();
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
	}
	
	@Override 
	public void onDetach(){
		super.onDetach();
	}
	
	private OnClickListener createOnClickListener () {
		return new OnClickListener () {
			@Override
			public void onClick(View v) { 
				//only create the board if a name has been entered
				if (mEditPostText.getText().toString() != null && 
						!mEditPostText.getText().toString().equals("")) {
					
		            //hide the keyboard
		            ((InputMethodManager) 
		            		getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
		            		.hideSoftInputFromWindow(mEditPostText.getWindowToken(), 0);

//					//TODO some sort of waiting animation
			        Toast.makeText(getActivity().getApplicationContext(), 
			        		"Sending...", Toast.LENGTH_LONG).show();
			        //send post to server
			        mMap.createPost(mEditPostText.getText().toString(), mBoard.getId());
				}
			}
		};
	}

	/**** FragContainerInterface handlers **********************/
	@Override
	public void setIsTextView(boolean isTextView) {
		if(this.isTextView != isTextView){
			this.isTextView = isTextView;
			
			changeFragment(isTextView);
		}
	}
}
